package pk.sistema.vendas.web.teste;

import static org.junit.Assert.*;

import org.junit.Test;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import pk.sistema.vendas.web.HibernateUtil;
import pk.sistema.vendas.web.cliente.Cliente;
import pk.sistema.vendas.web.cliente.ClienteRN;

public class ClienteTeste {

	 private static Session sessao;
		private static Transaction transacao;
		
		@BeforeClass
		public static void abreConexao(){
			sessao = HibernateUtil.getSessao().getCurrentSession();
			transacao = sessao.beginTransaction();
		}
		
		@AfterClass
		public static void fechaConexao(){
			//this.transacao.commit();
			
			try {
				transacao.commit();
				
			} catch (Throwable e) {
				System.out.println("Deu problema no Commit!!!"+ e.getMessage());
			}finally{
				try {
				
					if(sessao.isOpen()){
						sessao.close();
					}				
				} catch (Exception e2) {
					System.out.println("Deu problema ao fechar conexao!!!" +e2.getMessage());
				}
				
				}
			}
	
	@Before
	public void setup(){

		Cliente c1 = new Cliente("Mariana", "46886624245", "Rua b", 1000d, "mari@", new Date());
		Cliente c2 = new Cliente("Francisco", "46886624245", "Rua f", 2000d, "francisco@", new Date());
		Cliente c3 = new Cliente("Iraneide", "46886624245", "Rua c", 1500d, "iraneide", new Date());
		Cliente c4 = new Cliente("Hud", "46886624245", "Rua d", 5000d, "hud@", new Date());
		
		ClienteRN clienteRN = new ClienteRN();
		
		clienteRN.salvar(c1);
		clienteRN.salvar(c2);
		clienteRN.salvar(c3);
		clienteRN.salvar(c4);
		
	}

	@After
	public void limparBanco(){
		ClienteRN clienteRN = new ClienteRN();
		List<Cliente> lista	= clienteRN.listar();
		
		for (Cliente cliente : lista){
			clienteRN.excluir(cliente);;
		}
	}
	
	@Test
	public void salvarTeste(){
	
		Cliente c1 = new Cliente();
		
		c1.setNome("Paulo");
		c1.setCpf("46886624245");
		c1.setEndereco("Rua Treze");
		c1.setRenda(5200d);
		c1.setEmail("paulo@");
		c1.setDataCadastro(new Date());
		
		assertEquals(true, true);
	}
	
	@Test
	public void listarTeste(){
		ClienteRN clienteRN = new ClienteRN();
		List<Cliente> lista = clienteRN.listar();
	 	assertEquals(4, lista.size());
	}
	
	@Test
	public void excluirClienteTeste(){
		ClienteRN clienteRN = new ClienteRN();
		
		List<Cliente> lista = clienteRN.listar();
	
		Cliente clienteExcluido = lista.get(0);
		
		clienteRN.excluir(clienteExcluido);
		
		lista = clienteRN.listar();
		
		assertEquals(3, lista.size());
	}
	
	@Test
	public void pesquisarTeste(){
		ClienteRN clienteRN = new ClienteRN();
		
		Cliente clientePesquisado = clienteRN.pesquisar("Fra");
		
		assertEquals("francisco@", clientePesquisado.getEmail());
	}
	 

	@Test
	public void alterarTeste(){

		ClienteRN clienteRN = new ClienteRN();
		
		Cliente clientePesquisado = clienteRN.pesquisar("ud");
		
		assertEquals("hud@", clientePesquisado.getEmail());
		
		clientePesquisado.setEndereco("Este Endereço");
		
		clienteRN.alterar(clientePesquisado);
		
		Cliente clienteAlterado = clienteRN.pesquisar("ud");
		
		assertEquals("Este Endereço", clienteAlterado.getEndereco());
	}

	
	
}
